package com.collection.provider.service.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.collection.api.entity.sys.SysUser;
import com.collection.api.sys.SysUserService;
import com.collection.provider.biz.SysUserBiz;

@Service(version ="1.0.0")
public class SysUserServiceImpl implements SysUserService{

	@Autowired
	private SysUserBiz sysUserBiz;
	
	@Override
	public SysUser findByUsername(String userName) {
		if (StringUtils.isBlank(userName) || "".equals(userName)) {
            return null;
        }
        return sysUserBiz.findByUserName(userName, 0);
	}

	@Override
	public List<Map<String, Object>> findUserList(String userName, String realName, String mobile, String userType,
			Integer offset, Integer limit) {
		Map<String,Object> map = new HashMap<String,Object>();
		if(StringUtils.isNoneBlank(userName) && !"".equals(userName)){
			map.put("userName", userName);
		}
		if(StringUtils.isNoneBlank(realName) && !"".equals(realName)){
			map.put("realName", realName);
		}
		if(StringUtils.isNoneBlank(mobile) && !"".equals(mobile)){
			map.put("mobile", mobile);
		}
		if(StringUtils.isNoneBlank(userType) && !"".equals(userType)){
			map.put("userType", userType);
		}
		if(null != offset && !"".equals(offset)){
			map.put("offset", offset);
		}
		if(null != limit && !"".equals(limit)){
			map.put("limit", limit);
		}
		return sysUserBiz.findUserList(map);
	}

	@Override
	public int countUserList(String userName, String realName, String mobile, String userType) {
		Map<String,Object> map = new HashMap<String,Object>();
		if(StringUtils.isNoneBlank(userName) && !"".equals(userName)){
			map.put("userName", userName);
		}
		if(StringUtils.isNoneBlank(realName) && !"".equals(realName)){
			map.put("realName", realName);
		}
		if(StringUtils.isNoneBlank(mobile) && !"".equals(mobile)){
			map.put("mobile", mobile);
		}
		if(StringUtils.isNoneBlank(userType) && !"".equals(userType)){
			map.put("userType", userType);
		}
		return sysUserBiz.countUserList(map);
	}

	@Override
	public int countUserByMobile(String mobile) {
		if (StringUtils.isBlank(mobile) || "".equals(mobile)) {
            return 0;
        }
		return sysUserBiz.countUserByMobile(mobile);
	}

	@Override
	public int insertSelective(SysUser record) {
		return sysUserBiz.insertSelective(record);
	}

	@Override
	public SysUser selectByPrimaryKey(Integer id) {
		return sysUserBiz.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(SysUser record) {
		return sysUserBiz.updateByPrimaryKeySelective(record);
	}

	@Override
	public int lockedUser(Integer id) {
		 if (id == 1) {
            return 0;
         }
		return sysUserBiz.lockedUser(id);
	}

	@Override
	public int openUser(Integer id) {
		return sysUserBiz.openUser(id);
	}

	@Override
	public List<SysUser> getUserByParam(String realName, String mobile, String type, String locked, String roleId,
			String password, String nameOrMobile) {
		Map<String,Object> map = new HashMap<String,Object>();
		if(StringUtils.isNoneBlank(realName) && !"".equals(realName)){
			map.put("realName", realName);
		}
		if(StringUtils.isNoneBlank(mobile) && !"".equals(mobile)){
			map.put("mobile", mobile);
		}
		if(StringUtils.isNoneBlank(type) && !"".equals(type)){
			map.put("type", type);
		}
		if(null != locked && !"".equals(locked)){
			map.put("locked", locked);
		}
		if(null != roleId && !"".equals(roleId)){
			map.put("roleId", roleId);
		}
		if(StringUtils.isNoneBlank(password) && !"".equals(password)){
			map.put("password", password);
		}
		if(StringUtils.isNoneBlank(nameOrMobile) && !"".equals(nameOrMobile)){
			map.put("nameOrMobile", nameOrMobile);
		}
		return sysUserBiz.getUserByParam(map);
	}

}
